package com.example.admin.controller.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.admin.entity.SysPermission;
import com.example.admin.entity.SysUser;
import com.example.admin.service.ISysPermissionService;
import com.example.util.APIState;
import com.example.util.AjaxResult;
import com.example.util.CurrentUser;

/**
 * <p>
 * 资源权限信息表 前端控制器
 * </p>
 *
 * @author wangkai
 * @since 2019-06-04
 */
@Controller
@RequestMapping("/sys/sysPermission")
public class SysPermissionController {

	@Resource(name = "sysPermissionService")
	private ISysPermissionService service;

	/**
	 * 获取权限
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public AjaxResult list() {
		QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<SysPermission>();
		return new AjaxResult(APIState.S00, service.list(queryWrapper));
	}

	/**
	 * 保存
	 * 
	 * @param permission
	 * @return
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public AjaxResult save(SysPermission permission) {
		if (permission != null) {
			permission.setCreateDate(new Date());
			permission.setCreateUserId(CurrentUser.getAdminUser().getId());
			if (permission.insert()) {
				return new AjaxResult(APIState.S00, "保存成功");
			}
		}
		return new AjaxResult(APIState.S01, "保存失败");
	}

	/**
	 * 修改
	 * 
	 * @param permission
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public AjaxResult update(SysPermission permission) {
		if (permission != null && permission.getId() != null) {
			if (permission.insertOrUpdate()) {
				return new AjaxResult(APIState.S00, "保存成功");
			}
		}
		return new AjaxResult(APIState.S01, "保存失败");
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/delete")
	@ResponseBody
	public AjaxResult delete(@PathVariable Long id) {
		if (id != null) {
			SysPermission permission = service.getById(id);
			if (permission != null) {
				if (service.deleteParmission(permission)) {
					return new AjaxResult(APIState.S00, "保存成功");
				}
			} else {
				return new AjaxResult(APIState.S01, "当前权限不存在请重新刷新列表");
			}
		}
		return new AjaxResult(APIState.S01, "保存失败");
	}

	/**
	 * 当前用户权限菜单
	 * 
	 * @return
	 */
	@RequestMapping(value = "/list/getUserList")
	@ResponseBody
	public AjaxResult getUserList() {
		SysUser sysUser = CurrentUser.getAdminUser();
		List<SysPermission> list = service.getUserList(sysUser);
		List<SysPermission> result = new ArrayList<>();
		for (SysPermission sysPermission : list) {
			if (sysPermission.getFlag().intValue() == 1) {
				result.add(sysPermission);
			}
		}
		return new AjaxResult(APIState.S00, result);
	}

}
